package com.ck.server.controller.auth;

import com.ck.common.constant.AjaxResult;
import com.ck.common.entity.SysMenu;
import com.ck.common.entity.SysUser;
import com.ck.common.utils.security.SecurityUtils;
import com.ck.common.vo.LoginInfoVo;
import com.ck.framework.web.service.SysLoginService;
import com.ck.system.service.SysMenuService;
import com.ck.system.service.impl.SysPermissionServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author ck
 * @date 2023/3/23 10:04
 * desc:图像验证码登录、手机号登录
 */
@RestController
@Api(tags = "登录接口")
@Slf4j
@RequestMapping("/auth")
public class SysLoginController {

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private SysPermissionServiceImpl permissionService;

    @Autowired
    private SysMenuService menuService;


    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public AjaxResult login(@RequestBody LoginInfoVo user){
        AjaxResult result = loginService.loginForCaptcha(user);
//         AjaxResult result = loginService.loginForSms(user);
        log.info("result:{}",result.toString());
        return result ;
    }


    @GetMapping("/getInfo")
    @ApiOperation("获取用户信息")
    public AjaxResult getInfo(){
        //获取当前登陆用户
        SysUser user = SecurityUtils.getLoginUser().getUser();

        //角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        //权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);

        AjaxResult result = AjaxResult.success();
        result.put("user",user);
        result.put("roles",roles);
        result.put("permissions",permissions);
        return result;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    @ApiOperation("获取路由信息")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}

